<?php
class ThoigianchophepsController extends AppController {

	var $name = 'Thoigianchopheps';

	function index() {
		$this->Thoigianchophep->recursive = 0;
		$this->set('thoigianchopheps', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thoigianchophep', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thoigianchophep', $this->Thoigianchophep->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Thoigianchophep->create();
			if ($this->Thoigianchophep->save($this->data)) {
				$this->Session->setFlash(__('The thoigianchophep has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The thoigianchophep could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại thời gian này.', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
		  $this->data['Thoigianchophep']['moc1']=date('Y-m-d',strtotime($this->data['Thoigianchophep']['moc11']));
          $this->data['Thoigianchophep']['moc2']=date('Y-m-d',strtotime($this->data['Thoigianchophep']['moc21']));
			if ($this->Thoigianchophep->save($this->data)) {
				$this->Session->setFlash(__('Cập nhât thời gian thành công', true),'success');
			} else {
				$this->Session->setFlash(__('Cập nhật không thành công. vui long thử lại.', true),'error');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Thoigianchophep->read(null, $id);
            $this->data['Thoigianchophep']['moc11']= date('d-m-Y',strtotime($this->data['Thoigianchophep']['moc1']));
            $this->data['Thoigianchophep']['moc21']=date('d-m-Y',strtotime($this->data['Thoigianchophep']['moc2']));
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for thoigianchophep', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Thoigianchophep->delete($id)) {
			$this->Session->setFlash(__('Thoigianchophep deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Thoigianchophep was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
